package ch.subgate.pongwars;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

	private GameThread mThread;
	
	public GameView(Context context, AttributeSet attributes) {
		super(context, attributes);
		
		//Retrieve the surface holder and set the callback
		SurfaceHolder surfaceHolder = getHolder();
		surfaceHolder.addCallback(this);
		
		//Creating a thread without starting it yet
		mThread = new GameThread(surfaceHolder, context);
	} // Constructor
	
	@Override
	public boolean onTouchEvent(MotionEvent e) {
		return mThread.handleTouchEvent(e);
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		mThread.setSurfaceSize(width, height);
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
        this.getContext();
    }
	
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
        //Finally stopping the thread
        mThread.setRunning(false);
        try {
            mThread.join();
        } catch (InterruptedException e) {
            // do nothing
        }
    } // surfaceDestroyed
	
	public GameThread getThread() {
		return mThread;
	}

} // GameView
